var gulp = require('gulp')
var rename = require('gulp-rename')
var ts = require('gulp-typescript')
var tsProject = ts.createProject('tsconfig.json')

gulp.task('ts', function () {
  return gulp.src(['./src/**/**.ts','!./src/templates/**/**'])
    .pipe(tsProject())
    .pipe(gulp.dest('./dist'))
})

gulp.task('json', function () {
  return gulp.src(['./src/**/**.json'])
    .pipe(gulp.dest('./dist'))
})

gulp.task('watch', ['json', 'ts'], function () {
  gulp.watch('./src/**/**.ts', ['ts'])
  gulp.watch('./src/**/**.json', ['json'])
})

// create
gulp.task('logic', () => {
  var fileName = process.argv[3]

  if (0 !== fileName.indexOf('--')) {
    return new Error('错误：请输入文件名称 --logicName')
  }else {
    fileName = fileName.substr(2, fileName.length)
    return gulp.src(['./src/templates/templateLogic.ts'])
      .pipe(rename({dirname: '',basename: fileName}))
      .pipe(gulp.dest('./src/app/logics'))
  }
})
gulp.task('entity', () => {
  var fileName = process.argv[3]

  if (0 !== fileName.indexOf('--')) {
    return new Error('错误：请输入文件名称 --entityName')
  }else {
    fileName = fileName.substr(2, fileName.length)
    return gulp.src(['./src/templates/templateEntity.ts'])
      .pipe(rename({dirname: '',basename: fileName}))
      .pipe(gulp.dest('./src/app/entitys'))
  }
})

gulp.task('model', () => {
  var fileName = process.argv[3]

  if (0 !== fileName.indexOf('--')) {
    return new Error('错误：请输入文件名称 --modelName')
  }else {
    fileName = fileName.substr(2, fileName.length)
    return gulp.src(['./src/templates/templateModel.ts'])
      .pipe(rename({dirname: '',basename: fileName}))
      .pipe(gulp.dest('./src/app/models'))
  }
})

gulp.task('view', () => {
  var fileName = process.argv[3]

  if (0 !== fileName.indexOf('--')) {
    return new Error('错误：请输入文件名称 --viewName')
  }else {
    fileName = fileName.substr(2, fileName.length)
    return gulp.src(['./src/templates/templateView.ts'])
      .pipe(rename({dirname: '',basename: fileName}))
      .pipe(gulp.dest('./src/app/views'))
  }
})

gulp.task('controller', () => {
  var fileName = process.argv[3]

  if (0 !== fileName.indexOf('--')) {
    return new Error('错误：请输入文件名称 --ControllerName')
  }else {
    fileName = fileName.substr(2, fileName.length)
    return gulp.src(['./src/templates/templateController.ts'])
      .pipe(rename({dirname: '',basename: fileName}))
      .pipe(gulp.dest('./src/app/controllers'))
  }
})
